package com.magiccompass.alipay.barcode.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.magiccompass.alipay.barcode.entity.UserInfo;
import com.magiccompass.alipay.barcode.entity.ViewMerchantTransactionStatusRecords;
import com.magiccompass.alipay.barcode.model.MerchantTransactionSingleQueryModel;
import com.magiccompass.alipay.barcode.service.MerchantBusinessService;
import com.magiccompass.alipay.barcode.service.UserService;
import com.magiccompass.alipay.barcode.util.MerchantTransactionUtil;

@Controller
@RequestMapping(value="/bdQuery")
public class BusinessDataQueryController {
	
	@Resource(name="merchantBusinessService")
	private MerchantBusinessService merchantBusinessService;
	@Resource(name="userService")
	private UserService userService;
	
	@RequestMapping(value="/mtQuery", method = {RequestMethod.GET,RequestMethod.POST}, produces = "application/json; charset=UTF-8")
    public @ResponseBody String alipayBarcodeReverse(Model model,HttpServletRequest request) {
		String mtIdStr = request.getParameter("mtId");
		String returnJSON = "";
		List<ViewMerchantTransactionStatusRecords> mtStatusList = merchantBusinessService.findTransactionAllStatus(Integer.parseInt(mtIdStr));
		if(mtStatusList != null){
			ViewMerchantTransactionStatusRecords mtStatusView = mtStatusList.get(0);
			MerchantTransactionSingleQueryModel mtsqm = MerchantTransactionUtil.makeMerchantTransactionSingleQueryModel(mtStatusView);
			returnJSON = makeJSONString(mtsqm);
		}
		return returnJSON;
    }
	
	private String makeJSONString(MerchantTransactionSingleQueryModel mtsqm){
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		sb.append("\"mtId\":\""+mtsqm.getTransactionId().toString()+"\",");
		sb.append("\"status\":\""+mtsqm.getTransactionStatus()+"\",");
		sb.append("\"type\":\""+mtsqm.getTransactionType()+"\",");
		sb.append("\"create_time\":\""+mtsqm.getTransactionCreateTime()+"\",");
		UserInfo ui = userService.findUserInfoByUserId(mtsqm.getMakePaymentBy());
		if(ui != null){
			sb.append("\"make_payment_by\":\""+ui.getUserName()+"\",");
			sb.append("\"payment_request_time\":\""+mtsqm.getPaymentRequestTime()+"\",");
			sb.append("\"alipay_buyer_login_id\":\""+mtsqm.getAlipayBuyerLoginId()+"\",");
			sb.append("\"alipay_pay_time\":\""+mtsqm.getAlipayPayTime()+"\",");
		}else{
			sb.append("\"make_payment_by\":\"\",");
			sb.append("\"payment_request_time\":\"\",");
			sb.append("\"alipay_buyer_login_id\":\"\",");
			sb.append("\"alipay_pay_time\":\"\",");
		}
		ui = userService.findUserInfoByUserId(mtsqm.getCancelBy());
		if(ui != null){
			sb.append("\"cancel_by\":\""+ui.getUserName()+"\",");
			sb.append("\"cancel_request_time\":\""+mtsqm.getCancelRequestTime()+"\",");
			sb.append("\"alipay_cancel_time\":\""+mtsqm.getAlipayCancelTime()+"\",");
		}else{
			sb.append("\"cancel_by\":\"\",");
			sb.append("\"cancel_request_time\":\"\",");
			sb.append("\"alipay_cancel_time\":\"\",");
		}
		ui = userService.findUserInfoByUserId(mtsqm.getRefundBy());
		if(ui != null){
			sb.append("\"refund_by\":\""+ui.getUserName()+"\",");
			sb.append("\"refund_request_time\":\""+mtsqm.getRefundRequestTime()+"\",");
			sb.append("\"alipay_refund_time\":\""+mtsqm.getAlipayRefundTime()+"\",");
		}else{
			sb.append("\"refund_by\":\"\",");
			sb.append("\"refund_request_time\":\"\",");
			sb.append("\"alipay_refund_time\":\"\",");
		}
		ui = userService.findUserInfoByUserId(mtsqm.getReverseBy());
		if(ui != null){
			sb.append("\"reverse_by\":\""+ui.getUserName()+"\",");
			sb.append("\"reverse_request_time\":\""+mtsqm.getReverseRequestTime()+"\",");
			sb.append("\"alipay_reverse_time\":\""+mtsqm.getAlipayReverseTime()+"\",");
		}else{
			sb.append("\"reverse_by\":\"\",");
			sb.append("\"reverse_request_time\":\"\",");
			sb.append("\"alipay_reverse_time\":\"\",");
		}
		sb.append("\"uselessEndParameter\":\"\""); // to avoid additional comma
		sb.append("}");
		return sb.toString();
	}
}
